//引入express模块
const express = require('express');
const cors = require('cors')
//引入数据库连接模块
const pool = require('../pool.js');
//创建路由器对象
const r = express.Router();
//添加路由
const jwt = require('jsonwebtoken');
// const { id } = require('element-plus/es/locale/index.js');
const secret = "secret";
const exp = '1800s';

// 登录
r.get('/login', (req, res, next) => {
    const username = req.query.username;
    const password = req.query.password;
    const msql = `select * from lutian_user where username="${username}" and password="${password}"`
    pool.query(msql, (err, r) => {
        if (err) {
            next(err);
            return;
        } else if (r.length == 0) {
            res.send({ code: 200, msg: '用户名或密码错误' });
        } else {
            // let user = { username: username, password: password };
            // let token = jwt.sign(user, secret, { expiresIn: exp });
            res.send({ code: 200, msg: "登录成功", data:r[0] })

        }
    })
})

// 注册
r.post('/register', (req, res, next) => {
    const username = req.body.username;
    const password = req.body.password;
    const name = req.body.name;
    const phone = req.body.phone;
    const isadmin = req.body.isadmin;
    const msql = `insert into lutian_user set username="${username}",phone="${phone}",name="${name}",password="${password}",isadmin="${isadmin}",accounts="",accountsname=""`;
    const mysql1 = 'select * from lutian_user'
    pool.query(mysql1, (err, r) => {
        if (err) {
            next(err)
            return;
        }
        const existingUser = r.find(user => user.username === username)
        if (existingUser != undefined) {
            res.send({ code: 200, msg: '该用户名已经被注册' });
        } else {
            pool.query(msql, (err, r) => {
                if (err) {
                    next(err);
                    return;
                } else if (r.length == 0) {
                    res.send({ code: 400, msg: '注册失败' });
                } else {
                    res.send({ code: 200, msg: "注册成功" });
                    //console.log(r);
                }
            })
        }
    })

})

// 设置管理员
r.post('/setuserIsadmin', (req, res, next) => {
    const userId = req.body.userId;
    const isadmin = req.body.isadmin;
    const mysql = `update lutian_user set isadmin = ${isadmin} WHERE userId = ${userId}`;
    pool.query(mysql, (err, r) => {
        if (err) {
            next(err)
            return;
        } else {
            res.send({ code: 200, msg: '设置成功', data: r });
        }
    })

})

// 查询所有用户
r.get('/saveUser', (req, res, next) => {
    const msql = `SELECT * FROM lutian_user;`
    pool.query(msql, (err, r) => {
        if (err) {
            next(err);
            return;
        } else if (r.length == 0) {
            res.send({ code: 400, msg: '暂无用户' });
        } else {
            res.send({ code: 200, msg: "查询成功", data:r })

        }
    })
})

// 删除用户
r.post('/deleteUser', (req, res, next) => {
    const userId = parseInt(req.body.userId)
    const msql = `delete from lutian_user where userId = ${userId}`;
    pool.query(msql, (err, r) => {
        if (err) {
            next(err)
            return;
        }
        res.send({ code: 200, msg: "删除成功" });
    })

})





//暴露出去
module.exports = r;